<!DOCTYPE html>
<html lang="en">
<head>
    {template 'common/edithead'}
    <!--微擎 -->
    <!--这个放在最后一切平白安全-->
    <script type="text/javascript" src="./resource/js/require.js?v=20170915"></script>
</head>
<body class="nav-md" style="min-width: 800px">
<div>
    <div class="container xc_edit_from" >
        <div class="main_container" style="overflow-x: hidden;min-height: calc(100vh - 20px);background-color: white;width: 100%;">
            <div class="col-md-12 col-xs-12">
                <div class="x_panel">
                    <div class="x_content">
                        <br/>
                        <form class="form-horizontal" role="form" method="post" action="" name="submit">
                            <input type="hidden" name="xkey" value="{$xkey}">
                            <input type="hidden" name="xc[check_list]">
                            <div class="form-group">
                                <label class="col-sm-2 control-label">签到功能</label>
                                <div class="col-sm-8">
                                    <input type="checkbox" class="js-switch" value="1" name="xc[check_status]"
                                           data-value="{$xc['status']}" data-field="status">
                                </div>
                            </div>
                            <div class="form-group param" data-name="xc[check_list]">
                                <label  class="col-sm-2 control-label">签到积分</label>
                                <div class="col-sm-8">
                                    {if $xc['check_list']}
                                    {loop $xc['check_list'] $index $item}
                                    <div class="input-group">
                                         <span class="input-group-btn">
                                             <button class="btn btn-default" type="button">签到</button>
                                         </span>
                                        <input type="text" class="form-control" data-name="day" value="{$item['day']}">
                                        <span class="input-group-btn">
                                             <button class="btn btn-default" type="button">天 获得</button>
                                         </span>
                                        <input type="text" class="form-control" data-name="score" value="{$item['score']}">
                                         <span class="input-group-btn">
                                             <button class="btn btn-default" type="button">积分</button>
                                         </span>
                                        <span class="input-group-btn" onclick="parameter.parameter_add(this)">
                                            <button class="btn btn-default" type="button"><i class="fa fa-plus"></i></button>
                                        </span>
                                        <span class="input-group-btn" onclick="parameter.parameter_del(this)">
                                            <button class="btn btn-danger" type="button"><i class="fa fa-remove"></i></button>
                                        </span>
                                    </div>
                                    {/loop}
                                    {else}
                                    <div class="input-group">
                                         <span class="input-group-btn">
                                             <button class="btn btn-default" type="button">签到</button>
                                         </span>
                                        <input type="text" class="form-control" data-name="day" value="">
                                        <span class="input-group-btn">
                                             <button class="btn btn-default" type="button">天 获得</button>
                                         </span>
                                        <input type="text" class="form-control" data-name="score" value="">
                                         <span class="input-group-btn">
                                             <button class="btn btn-default" type="button">积分</button>
                                         </span>
                                        <span class="input-group-btn" onclick="parameter.parameter_add(this)">
                                            <button class="btn btn-default" type="button"><i class="fa fa-plus"></i></button>
                                        </span>
                                        <span class="input-group-btn" onclick="parameter.parameter_del(this)">
                                            <button class="btn btn-danger" type="button"><i class="fa fa-remove"></i></button>
                                        </span>
                                    </div>
                                    {/if}
                                </div>
                            </div>
                            <script>
                                var parameter={
                                    parameter_add:function(objc){
                                        $(objc).parent().after('<div class="input-group">' +
                                                '<span class="input-group-btn">' +
                                                '<button class="btn btn-default" type="button">签到</button>' +
                                                '</span>' +
                                                '<input type="text" class="form-control" data-name="day" value="">' +
                                                '<span class="input-group-btn">' +
                                                '<button class="btn btn-default" type="button">天 获得</button>' +
                                                '</span>' +
                                                '<input type="text" class="form-control" data-name="score" value="">' +
                                                '<span class="input-group-btn">' +
                                                '<button class="btn btn-default" type="button">积分</button>' +
                                                '</span>' +
                                                '<span class="input-group-btn" onclick="parameter.parameter_add(this)">' +
                                                '<button class="btn btn-default" type="button"><i class="fa fa-plus"></i></button>' +
                                                '</span>' +
                                                '<span class="input-group-btn" onclick="parameter.parameter_del(this)">' +
                                                '<button class="btn btn-danger" type="button"><i class="fa fa-remove"></i></button>' +
                                                '</span>' +
                                                '</div>')
                                    },
                                    parameter_del:function(objc){
                                        if($(objc).parent().siblings().length>0){
                                            $(objc).parent().remove();
                                        }
                                    }
                                }
                            </script>
                            <div class="form-group">
                                <div class="col-sm-offset-2 col-sm-10">
                                    <input type="button" name="submit" class="btn btn-default" value="提交">
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{template 'common/editfoot'}
<script>
    $(function(){
        $("input[name='submit']").click(function(){
            getJson();
            var data=$(".form-horizontal").serialize();
            $.ajax({
                type:"post",
                url:"{url 'site/entry/'.$_GPC['do'],array('m'=>$_GPC['m'],'op'=>'score_save','version_id'=>$_GPC['version_id'])}",
                data:data,
                dataType:'json',
                success:function(res){
                    if(res.status==1){
                        swal('操作成功!', '操作成功!', 'success');
                    }else{
                        swal('操作失败!', '操作失败!', 'error');
                    }
                }
            })
        });
    });
    function getJson(){
        $(".param").each(function(){
            var name=$(this).attr("data-name");
            var data=[];
            $(this).find(".input-group").each(function(){
                var data_item={};
                $(this).find("input").each(function(){
                    if($(this).val()!=""){
                        data_item[$(this).attr("data-name")]=$(this).val();
                    }
                });
                if(JSON.stringify(data_item)!="{}"){
                    data.push(data_item);
                }
            });
            if(data.length>0){
                $("input[name='"+name+"']").val(JSON.stringify(data));
            }
        });
    }
</script>
</body>
</html>